package com.guldan.core.jackson;

/**
 * Created by Silas.
 * Date: 2016/10/1
 * Time: 22:02
 */

import com.guldan.core.jackson.SignedMoneySerialize;
import com.fasterxml.jackson.annotation.JsonFormat.Value;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;

public class UnitSuffixSerialize extends JsonSerializer<Number> implements ContextualSerializer {
    protected String suffix;
    protected String prefix;
    protected NumberSerializer numberSerializer = new NumberSerializer(Number.class);

    public void serialize(Number value, JsonGenerator jgen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        if(StringUtils.isBlank(this.prefix) && StringUtils.isBlank(this.suffix)) {
            this.numberSerializer.serialize(value, jgen, serializers);
        } else if(StringUtils.isNotBlank(this.prefix)) {
            jgen.writeString(this.prefix + value);
        } else if(StringUtils.isNotBlank(this.suffix)) {
            jgen.writeString(value + this.suffix);
        } else {
            jgen.writeString(this.suffix + value + this.suffix);
        }

    }

    public UnitSuffixSerialize() {
    }

    public UnitSuffixSerialize(String prefix, String suffix) {
        this.prefix = prefix;
        this.suffix = suffix;
    }

    public UnitSuffixSerialize getSerializer(String p, String s, String t) {
        return (UnitSuffixSerialize)("money".equals(t)?new SignedMoneySerialize(p, s):new UnitSuffixSerialize(p, s));
    }

    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
        if(property != null) {
            Value format = prov.getAnnotationIntrospector().findFormat(property.getMember());
            if(format != null) {
                if(!format.hasPattern()) {
                    return new UnitSuffixSerialize((String)null, (String)null);
                }

                String pattern = format.getPattern();
                String[] arr = pattern.split(",");
                if(arr.length < 1) {
                    return new UnitSuffixSerialize((String)null, (String)null);
                }

                String p = null;
                String s = null;
                String t = null;
                String[] arr$ = arr;
                int len$ = arr.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    String string = arr$[i$];
                    String[] split = string.split("=");
                    if(split.length >= 2) {
                        if("p".equalsIgnoreCase(split[0])) {
                            p = split[1];
                        } else if("s".equalsIgnoreCase(split[0])) {
                            s = split[1];
                        } else if("t".equalsIgnoreCase(split[0])) {
                            t = split[1];
                        }
                    }
                }

                return this.getSerializer(p, s, t);
            }
        }

        return this;
    }
}
